//package com.luli.config.linuxData;
//
///**
// * 陆离
// * 2024/12/26
// */
//import com.jcraft.jsch.Channel;
//import com.jcraft.jsch.ChannelExec;
//import com.jcraft.jsch.JSch;
//import com.jcraft.jsch.Session;
//import java.io.BufferedReader;
//import java.io.InputStream;
//import java.io.InputStreamReader;
//import java.util.ArrayList;
//import java.util.List;
//
//public class LinuxNetworkStatusGetterViaSSH {
//    public static class NetworkInterfaceStatus {
//        String interfaceName;
//        String status;
//
//        public NetworkInterfaceStatus(String interfaceName, String status) {
//            this.interfaceName = interfaceName;
//            this.status = status;
//        }
//
//        @Override
//        public String toString() {
//            return "网络接口 " + interfaceName + " 的状态为: " + status;
//        }
//    }
//
//    public static List<NetworkInterfaceStatus> getNetworkInterfaceStatus(String host, int port, String user, String password) {
//        List<NetworkInterfaceStatus> statusList = new ArrayList<>();
//        try {
//            JSch jsch = new JSch();
//            Session session = jsch.getSession(user, host, port);
//            session.setPassword(password);
//            session.setConfig("StrictHostKeyChecking", "no");
//            session.connect();
//            Channel channel = session.openChannel("exec");
//            ((ChannelExec) channel).setCommand("ip -o link show");
//            channel.connect();
//            InputStream in = channel.getInputStream();
//            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
//            String line;
//            while ((line = reader.readLine())!= null) {
//                String[] parts = line.split(":");
//                String interfaceName = parts[1].trim();
//                String status = parts[2].trim();
//                statusList.add(new NetworkInterfaceStatus(interfaceName, status));
//            }
//            channel.disconnect();
//            session.disconnect();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return statusList;
//    }
//
//    public static boolean checkInternetConnectivity(String host, int port, String user, String password) {
//        try {
//            JSch jsch = new JSch();
//            Session session = jsch.getSession(user, host, port);
//            session.setPassword(password);
//            session.setConfig("StrictHostKeyChecking", "no");
//            session.connect();
//            Channel channel = session.openChannel("exec");
//            ((ChannelExec) channel).setCommand("ping -c 3 8.8.8.8");
//            channel.connect();
//            InputStream in = channel.getInputStream();
//            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
//            String line;
//            boolean pingSuccess = false;
//            while ((line = reader.readLine())!= null) {
//                if (line.contains("3 packets transmitted, 3 received")) {
//                    pingSuccess = true;
//                }
//            }
//            channel.disconnect();
//            session.disconnect();
//            return pingSuccess;
//        } catch (Exception e) {
//            e.printStackTrace();
//            return false;
//        }
//    }
//
//    /**
//     * 校验服务器是否网络正常
//     *
//     * @return
//     */
//    public static boolean checkInternetConnectivity(Channel channel,Session session) {
//        try {
//            //谷歌的公共 DNS 服务器
//            ((ChannelExec) channel).setCommand("ping -c 3 8.8.8.8");
//            channel.connect();
//            InputStream in = channel.getInputStream();
//            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
//            String line;
//            boolean pingSuccess = false;
//            while ((line = reader.readLine()) != null) {
//                if (line.contains("3 packets transmitted")) {
//                    pingSuccess = true;
//                }
//            }
//            return pingSuccess;
//        } catch (Exception e) {
//            return false;
//        }
//    }
//
//    public static void main(String[] args) {
//        String host = "192.168.175.135";
//        int port = 22;
//        String user = "root";
//        String password = "lzp+19991128";
//        List<NetworkInterfaceStatus> interfaceStatusList = getNetworkInterfaceStatus(host, port, user, password);
//        for (NetworkInterfaceStatus status : interfaceStatusList) {
//            System.out.println("----------" + status);
//        }
//        boolean internetConnectivity = checkInternetConnectivity(host, port, user, password);
//        if (internetConnectivity) {
//            System.out.println("服务器可以连接到互联网");
//        } else {
//            System.out.println("服务器可能无法连接到互联网");
//        }
//    }
//}
